<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
class User extends Mycontroller
{

    public function __construct(){
        parent::__construct();
    }

    public function index(){

        return $this->fetch();
    }

    public function edituser(){
        $data = [
            'id'=>$_POST['id'],
            'name'=>$_POST['name'],
            'phone'=>$_POST['phone'],
        ];
        //判断是否有上传头像
        if($_FILES['imgurl']['error']===0){
            // 获取表单上传文件 例如上传了001.jpg
            $file = request()->file('imgurl');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
            // 成功上传后 获取上传信息
            $path = $info->getSaveName();
            //转换路径
            $path = str_replace('\\', '/', $path);
            $data = [
                'id'=>$_POST['id'],
                'name'=>$_POST['name'],
                'imgurl'=>$path,
                'phone'=>$_POST['phone'],
            ];
            }else{
            // 上传失败获取错误信息
            echo  "<script>alert('上传文件失败!');history.back()</script>";
            return ;
            }
        }
        $res = db('user')->update($data);
        if($res){
            echo "<script>alert('操作成功!');</script>";
            return $this->fetch('user/person');
        }else{
            echo  "<script>alert('失败!');history.back()</script>";
            return ;
        }
    }

    public function person(){
        return $this->fetch('User/person');
    }

    public function goeditpass(){
        return $this->fetch('User/editpass');
    }

    public function editpass(){
        // var_dump(input(''));die;
        //验证数据
        foreach($_POST as $v){
            if(empty($v)){
                echo  "<script>alert('数据不能为空');history.back()</script>";
                return ;
            }
        }
        if($_POST['newpass']!==$_POST['repass']){
            echo  "<script>alert('两次密码不一致');history.back()</script>";
            return ;
        }
        $yz = db('user')->where('id','=',$_POST['id'])->where('pass','=',$_POST['oldpass'])->find();
        if(!$yz){
            echo  "<script>alert('原始密码错误');history.back()</script>";
            return ;
        }
        $data=[
            'id'=>$_POST['id'],
            'pass'=>$_POST['repass']
        ];
        $res = db('user')->update($data);
        if($res){
            echo  "<script>alert('更改成功');</script>";
            return  action('UserIndex/Index');
        }else{
            echo  "<script>alert('操作失败');history.back()</script>";
            return ;
        }
    }

    public function gopenname(){
        return $this->fetch('User/editpenname');
    }


    public function editpenname(){
        if($_FILES['picurl']['error']===0){
            // 获取表单上传文件 例如上传了001.jpg
            $file = request()->file('picurl');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                $path = $info->getSaveName();
                //转换路径
                $path = str_replace('\\', '/', $path);
                $data=[
                    'id'=>$_POST['id'],
                    'picurl'=>$path
                ];
                $res = db('user')->update($data);
                if($res){
                    echo  "<script>alert('更改成功');</script>";
                    return  action('UserIndex/Index');
                }
            }
        }
        echo  "<script>alert('操作失败');history.back()</script>";
        return ;
    }

}